#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests
import pandas as pd
import json
pd.set_option('display.width', 5000)
url="http://web.juhe.cn:8080/finance/stock/usaall"
list
stock_result={}
def stock_info(page):
    print("page",page)
    pamarms={
        "page":page,
        "type":"3",
        "key":"f8451274716ae0820c60b13cfb9e956e"
    }
    try:
        res=requests.get(url,params=pamarms)
        if res.status_code==200:
            return res.text
        else:
            stock_info(page)
    except Exception as e:
        print(e)
        stock_info(page)
def parse_result(re):
    result=json.loads(re)
    code = result.get("error_code")
    final_reult = result.get("result")
    page = final_reult.get("page")
    datas = final_reult.get("data")
    print("code",code,"page",page,"datas",len(datas))
    for data in datas:
        name = data.get("cname")
        symbol = data.get("symbol")
        stock_result[name] = symbol

for i in range(1,150):
    res = stock_info(i)
    parse_result(res)

print(len(stock_result))
with open("stock_us.json","w",encoding="utf-8") as f:
    f.write(str(stock_result))




